<template>
    <div class="app-container">
        <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch"
            label-width="68px">
            <el-form-item label="车辆名称" prop="vehicleName">
                <el-input v-model="queryParams.vehicleName" placeholder="请输入车辆名称" clearable
                    @keyup.enter.native="handleQuery" />
            </el-form-item>
            <el-form-item label="车牌号" prop="plateNumber">
                <el-input v-model="queryParams.plateNumber" placeholder="请输入车牌号" clearable
                    @keyup.enter.native="handleQuery" />
            </el-form-item>
            <el-form-item label="客户名称" prop="customerName">
                <el-input v-model="queryParams.customerName" placeholder="请输入客户名称" clearable
                    @keyup.enter.native="handleQuery" />
            </el-form-item>
            <el-form-item label="定损状态" prop="damageStatus">
                <el-select v-model="queryParams.damageStatus" clearable placeholder="请选择">
                    <el-option v-for="item in damageOptions" :key="item.value" :label="item.label" :value="item.value">
                    </el-option>
                </el-select>
            </el-form-item>
            <el-form-item label="发票状态" prop="invoiceStatus">
                <el-select v-model="queryParams.invoiceStatus" clearable placeholder="请选择">
                    <el-option v-for="item in invoiceOptions" :key="item.value" :label="item.label" :value="item.value">
                    </el-option>
                </el-select>
            </el-form-item>
            <el-form-item>
                <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
                <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
            </el-form-item>
        </el-form>

        <el-row :gutter="10" class="mb8">
            <el-col :span="1.5">
                <el-button type="primary" plain icon="el-icon-plus" size="mini" @click="handleAdd">新增</el-button>
            </el-col>
            <el-col :span="1.5">
                <el-button type="success" plain icon="el-icon-edit" size="mini" :disabled="single"
                    @click="handleUpdate">修改</el-button>
            </el-col>
            <el-col :span="1.5">
                <el-button type="danger" plain icon="el-icon-delete" size="mini" :disabled="multiple"
                    @click="handleDelete">删除</el-button>
            </el-col>
            <!-- <el-col :span="1.5">
          <el-button type="warning" plain icon="el-icon-download" size="mini" @click="handleExport">导出</el-button>
        </el-col> -->
            <!-- <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar> -->
        </el-row>

        <el-table v-loading="loading" :data="workOrderList" @selection-change="handleSelectionChange">
            <el-table-column type="selection" width="55" align="center" />
            <el-table-column label="车辆名称" align="center" prop="vehicleName" />
            <el-table-column label="车牌号" align="center" prop="plateNumber" />
            <el-table-column label="责任" align="center" prop="duty" />
            <el-table-column label="客户名称" align="center" prop="customerName" />
            <el-table-column label="维修内容" align="center" prop="repairContent" />
            <el-table-column label="定损状态" align="center" prop="damageStatus">
                <template slot-scope="scope">
                    <el-switch v-model="scope.row.damageStatus" active-color="#13ce66" inactive-color="#ff4949"
                        :active-value="1" :inactive-value="0" disabled>
                    </el-switch>
                </template>
            </el-table-column>
            <el-table-column label="定损员" align="center" prop="damageStaff" />
            <el-table-column label="定损金额" align="center" prop="damageAmount" />
            <el-table-column label="发票状态" align="center" prop="invoiceStatus">
                <template slot-scope="scope">
                    <el-switch v-model="scope.row.invoiceStatus" active-color="#13ce66" inactive-color="#ff4949"
                        :active-value="1" :inactive-value="0" disabled>
                    </el-switch>
                </template>
            </el-table-column>
            <el-table-column label="施救费用" align="center" prop="rescueAmount" />
            <el-table-column label="实收金额" align="center" prop="receiveAmount" />
            <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
                <template slot-scope="scope">
                    <el-button size="mini" type="text" icon="el-icon-edit"
                        @click="handleUpdate(scope.row)">修改</el-button>
                    <el-button size="mini" type="text" icon="el-icon-delete"
                        @click="handleDelete(scope.row)">删除</el-button>
                </template>
            </el-table-column>
        </el-table>

        <pagination v-show="total > 0" :total="total" :page.sync="queryParams.pageNum"
            :limit.sync="queryParams.pageSize" @pagination="getList" />

        <!-- 添加或修改物品对话框 -->
        <el-dialog :title="title" :visible.sync="open" width="800px" append-to-body @close="cancel"
            :close-on-click-modal="false" top="5vh">
            <el-form ref="form" :model="form" :rules="rules" label-width="80px">
                <el-row>
                    <el-col :span="12">
                        <el-form-item label="车辆名称" prop="vehicleName">
                            <el-input v-model="form.vehicleName" placeholder="请输入车辆名称" />
                        </el-form-item>
                    </el-col>
                    <el-col :span="12">
                        <el-form-item label="车牌号" prop="plateNumber">
                            <el-input v-model="form.plateNumber" placeholder="请输入车牌号" />
                        </el-form-item>
                    </el-col>
                </el-row>
                <el-row>
                    <el-col :span="12">
                        <el-form-item label="客户名称" prop="customerName">
                            <el-input v-model="form.customerName" placeholder="请输入客户名称" />
                        </el-form-item>
                    </el-col>
                    <el-col :span="12">
                        <el-form-item label="责任" prop="duty">
                            <el-input v-model="form.duty" placeholder="请输入责任" />
                        </el-form-item>
                    </el-col>
                </el-row>
                <el-row>
                    <el-col :span="12">
                        <el-form-item label="维修内容" prop="repairContent">
                            <el-input type="textarea" :rows="2" v-model="form.repairContent" placeholder="请输入维修内容" />
                        </el-form-item>
                    </el-col>
                </el-row>
                <el-form-item label="图片">
                    <image-upload @input="handleImageUpload" :limit="10" :value="form.image" />
                </el-form-item>
                <el-row>
                    <el-col :span="12">
                        <el-form-item label="定损状态" prop="damageStatus">
                            <el-switch v-model="form.damageStatus" active-color="#13ce66" inactive-color="#ff4949"
                                :active-value="1" :inactive-value="0">
                            </el-switch>
                        </el-form-item>
                    </el-col>
                </el-row>
                <el-row>
                    <el-col :span="12">
                        <el-form-item label="定损员" prop="damageStaff">
                            <el-input v-model="form.damageStaff" placeholder="请输入定损员" />
                        </el-form-item>
                    </el-col>
                    <el-col :span="12">
                        <el-form-item label="定损金额" prop="damageAmount">
                            <el-input-number v-model="form.damageAmount" :precision="2" :step="100" :min="0" />
                            <!-- <el-input v-model="form.damageAmount" placeholder="请输入定损金额" /> -->
                        </el-form-item>
                    </el-col>
                </el-row>
                <el-row>
                    <el-col :span="12">
                        <el-form-item label="发票状态" prop="invoiceStatus">
                            <el-switch v-model="form.invoiceStatus" active-color="#13ce66" inactive-color="#ff4949"
                                :active-value="1" :inactive-value="0">
                            </el-switch>
                        </el-form-item>
                    </el-col>
                </el-row>
                <el-row>
                    <el-col :span="12">
                        <el-form-item label="施救费用" prop="rescueAmount">
                            <el-input-number v-model="form.rescueAmount" :precision="2" :step="100" :min="0" />
                        </el-form-item>
                    </el-col>
                    <el-col :span="12">
                        <el-form-item label="实收金额" prop="receiveAmount">
                            <el-input-number v-model="form.receiveAmount" :precision="2" :step="100" :min="0" />
                        </el-form-item>
                    </el-col>
                </el-row>
            </el-form>
            <div slot="footer" class="dialog-footer">
                <el-button type="primary" @click="submitForm">确 定</el-button>
                <el-button @click="cancel">取 消</el-button>
            </div>
        </el-dialog>
    </div>
</template>

<script>
import { listWorkOrder, getWorkOrder, delWorkOrder, addWorkOrder, updateWorkOrder } from "@/api/workOrder"
import ImagePreview from "@/components/ImagePreview"
import Pagination from "@/components/Pagination"
import ImageUpload from "@/components/ImageUpload"

export default {
    name: "WorkOrder",
    components: {
        ImagePreview,
        Pagination,
        ImageUpload
    },
    data() {
        return {
            // 遮罩层
            loading: true,
            // 选中数组
            ids: [],
            // 非单个禁用
            single: true,
            // 非多个禁用
            multiple: true,
            // 显示搜索条件
            showSearch: true,
            // 总条数
            total: 0,
            // 物品表格数据
            workOrderList: [],
            // 弹出层标题
            title: "",
            // 是否显示弹出层
            open: false,
            // 查询参数
            queryParams: {
                pageNum: 1,
                pageSize: 10,
                totalRow: -1,
                vehicleName: null,
                plateNumber: null,
                customerName: null,
                damageStatus: null,
                invoiceStatus: null
            },
            damageOptions:[{ label: '未定损', value: 0 }, { label: '已定损', value: 1 }],
            invoiceOptions:[{ label: '未开发票', value: 0 }, { label: '已开发票', value: 1 }],
            // unitOptions: [{ label: '年', key: 1 }, { label: '月', key: 2 }, { label: '日', key: 3 }],
            // 表单参数
            form: {},
            // uploadImgUrl: process.env.VUE_APP_BASE_API + "/common/upload",
            // fileList: [],
            // 表单校验
            rules: {
                vehicleName: [
                    { required: true, message: "车辆名称不能为空", trigger: "blur" }
                ],
                plateNumber: [
                    { required: true, message: "车牌号不能为空", trigger: "blur" }
                ]
            }
        }
    },
    created() {
        this.getList()
    },
    methods: {
        /** 查询物品列表 */
        getList() {
            this.loading = true
            listWorkOrder(this.queryParams).then(response => {
                this.workOrderList = response.data.records
                this.total = response.data.totalRow
                this.queryParams.totalRow = response.data.totalRow
                this.loading = false
            })
        },
        // 取消按钮
        cancel() {
            this.open = false
            this.reset()
        },
        // 表单重置
        reset() {
            this.form = {
                id: null,
                vehicleName: null,
                plateNumber: null,
                duty: null,
                customerName: null,
                image: null,
                damageStatus: 0,
                damageStaff: null,
                damageAmount: null,
                invoiceStatus: 0,
                rescueAmount: null,
                receiveAmount: null
            }
            // this.fileList = []
            // this.resetForm("form")

        },
        /** 搜索按钮操作 */
        handleQuery() {
            this.queryParams.pageNum = 1
            this.queryParams.totalRow = -1
            this.getList()
        },
        /** 重置按钮操作 */
        resetQuery() {
            this.$refs["queryForm"].resetFields();
            this.handleQuery()
        },
        // 多选框选中数据
        handleSelectionChange(selection) {
            this.ids = selection.map(item => item.id)
            this.single = selection.length !== 1
            this.multiple = !selection.length
        },
        /** 新增按钮操作 */
        handleAdd() {
            this.reset()
            this.open = true
            this.title = "添加工单"
        },
        /** 修改按钮操作 */
        handleUpdate(row) {
            const id = row.id || this.ids
            getWorkOrder(id).then(response => {
                this.form = response.data
                this.open = true
                this.title = "修改工单"
            })
        },
        /** 提交按钮 */
        submitForm() {
            this.$refs["form"].validate(valid => {
                if (valid) {
                    this.queryParams.categoryId = null;
                    if (this.form.id != null) {
                        updateWorkOrder(this.form).then(response => {
                            this.$modal.msgSuccess("修改成功")
                            this.open = false
                            this.getList()
                        })
                    } else {
                        addWorkOrder(this.form).then(response => {
                            this.$modal.msgSuccess("新增成功")
                            this.open = false
                            this.getList()
                        })
                    }
                }
            })
        },
        /** 删除按钮操作 */
        handleDelete(row) {
            const ids = row.id || this.ids
            this.$modal.confirm('是否确认删除数据？').then(function () {
                return delWorkOrder(ids)
            }).then(() => {
                this.getList()
                this.$modal.msgSuccess("删除成功")
            }).catch(() => { })
        },
        handleImageUpload(url) {
            this.form.image = url
        },
        /** 导出按钮操作 */
        // handleExport() {
        //   this.download('flex/item/export', {
        //     ...this.queryParams
        //   }, `item_${new Date().getTime()}.xlsx`)
        // }
    }
}
</script>